//
//  MAPServerRequest.m
//  Kjs
//
//  Created by cfq on 16/5/25.
//  Copyright © 2016年 kjs. All rights reserved.
//

#import "MAPServerRequest.h"
#import <AFNetworking.h>
#import <AFHTTPSessionManager.h>
#import <MBProgressHUD.h>
#import "AppDelegate.h"

@implementation MAPServerRequest
+ (void) GETWithURL:(NSString *)URL parameters:(id)parameters success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))successResponse failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failureResponse inView:(UIView *)view{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (view == nil) {
        view = [UIApplication sharedApplication].delegate.window;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    NSLog(@"request URL : %@",URL);
    [manager GET:URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject: %@",responseObject);
        successResponse(task, responseObject);
        [hud hide:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"---------- request error ----------\n");
        NSLog(@"get request error: %@",error);
        failureResponse(task, error);
        [hud hide:YES];
    }];
}

/**
 如果遇到content-type 是 application/text的形式
 应该设置manager.requestSerializer = [AFJSONRequestSerializer serializer];
 */
+ (void)POSTWithURL:(nonnull NSString *)URL
         paramaters:(nullable id)parameters
            success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
            failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
             inView:(nullable UIView *)view
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 30;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    if(view == nil) {
        view = [UIApplication sharedApplication].delegate.window;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    
    NSLog(@"request URL : %@",URL);
    [manager
     POST:URL
     parameters:parameters
     progress:nil
     success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"responseObject: %@",responseObject);
         success(task,responseObject);
         [hud hide:YES];
     }
     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"---------- request error ----------\n");
         NSLog(@"post request error: %@",error);
         failure(task,error);
         [hud hide:YES];
     }];
}

+ (void)silencedGETWithURL:(NSString *)URL parameters:(id)parameters success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))successResponse failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failureResponse {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSLog(@"request URL : %@",URL);
    [manager
     GET:URL
     parameters:parameters
     progress:nil
     success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"responseObject: %@",responseObject);
         successResponse(task,responseObject);
     }
     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"---------- request error ----------\n");
         NSLog(@"get request error: %@",error);
         //         failureResponse(task,error);
     }];
    
}

+ (void)silencedPOSTWithURL:(NSString *)URL paramaters:(id)parameters success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 30;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    NSLog(@"request URL : %@",URL);
    [manager
     POST:URL
     parameters:parameters
     progress:nil
     success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"responseObject: %@",responseObject);
         success(task,responseObject);
     }
     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"---------- request error ----------\n");
         NSLog(@"post request error: %@",error);
         failure(task,error);
     }];
}

@end
